import streamlit as st
from PIL import Image

from entity.face_types import ValidMetrics

# 页面配置
st.set_page_config(
    page_title="Media Processing WebUI",
    page_icon="🖥️",
    layout="wide"
)

# 侧边栏导航
st.sidebar.title("功能导航")
function = st.sidebar.radio(
    "选择功能模块",
    ["人脸特征提取", "人脸匹配", "语音识别", "视频处理", "系统状态"]
)

# 主内容区
st.title("媒体处理服务Web界面")
st.write("---")

# 功能模块路由
if function == "人脸特征提取":
    st.header("🎭 人脸特征提取")
    with st.expander("ℹ️ 功能说明"):
        st.write("上传包含人脸的图片，提取人脸特征向量")
    
    # 图片上传区域
    col1, col2 = st.columns([1, 1])
    with col1:
        uploaded_file = st.file_uploader(
            "上传图片", 
            type=["jpg", "jpeg", "png"],
            key="face_feature_upload"
        )
    
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        with col2:
            st.image(image, caption="上传的图片", use_column_width=True)
        
        # 参数设置
        with st.expander("⚙️ 参数设置"):
            detection_model = st.selectbox(
                "检测模型",
                ["retinaface", "mtcnn", "dlib"],
                index=0,
                help="选择人脸检测使用的模型"
            )
            extract_model = st.selectbox(
                "特征提取模型",
                ["arcface", "facenet", "vggface"],
                index=0,
                help="选择特征提取使用的模型"
            )
        
        # 执行按钮
        if st.button("🚀 提取特征", key="extract_feature_btn"):
            with st.spinner("🔍 正在提取人脸特征..."):
                # TODO: 调用API
                st.success("✅ 特征提取完成！")
                # 结果展示
                st.json({
                    "status": "success",
                    "feature_vector": [0.1, 0.2, 0.3],  # 示例数据
                    "message": "特征提取成功"
                })

elif function == "人脸匹配":
    st.header("👥 人脸匹配")
    with st.expander("ℹ️ 功能说明"):
        st.write("上传两组人脸图片，计算相似度")
    
    # 图片上传区域
    col1, col2 = st.columns([1, 1])
    with col1:
        st.subheader("第一组图片")
        group1_files = st.file_uploader(
            "上传第一组图片", 
            type=["jpg", "jpeg", "png"],
            accept_multiple_files=True,
            key="group1_upload"
        )
    with col2:
        st.subheader("第二组图片")
        group2_files = st.file_uploader(
            "上传第二组图片", 
            type=["jpg", "jpeg", "png"],
            accept_multiple_files=True,
            key="group2_upload"
        )
    
    if group1_files and group2_files:
        # 参数设置
        with st.expander("⚙️ 参数设置"):
            metric = st.selectbox(
                "相似度度量方法",
                ValidMetrics,
                index=0
            )
            threshold = st.slider(
                "相似度阈值",
                0.0, 1.0, 0.6
            )
        
        # 执行按钮
        if st.button("🚀 计算相似度", key="match_faces_btn"):
            with st.spinner("🔍 正在计算相似度..."):
                # TODO: 调用API
                st.success("✅ 相似度计算完成！")
                # 结果展示
                st.json({
                    "status": "success",
                    "matches": [
                        {"image1": "img1.jpg", "image2": "img2.jpg", "similarity": 0.85},
                        {"image1": "img1.jpg", "image2": "img3.jpg", "similarity": 0.72}
                    ],
                    "message": "匹配完成"
                })

elif function == "语音识别":
    st.header("🎙️ 语音识别")
    st.info("?? 功能开发中，敬请期待...")

elif function == "视频处理":
    st.header("🎬 视频处理")
    st.info("📌 功能开发中，敬请期待...")

elif function == "系统状态":
    st.header("📊 系统状态")
    st.info("📌 功能开发中，敬请期待...")

# 页脚
st.write("---")
st.caption("Media Processing WebUI © 2023")
